import { Component, OnInit } from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import {HttpService} from '@core/services/http.service';

@Component({
  selector: 'app-map',
  templateUrl: './map.component.html',
  styleUrls: ['./map.component.less']
})
export class MapComponent implements OnInit {

  componentList = [];
  projectParmas = {
      width: '',
      height: ''
  };
  pIndex = 0;
  p_id;
  role = JSON.parse(localStorage.getItem('_user')).role;

  constructor(public activeRouter: ActivatedRoute, public Http: HttpService) { }

  ngOnInit() {
      this.activeRouter.queryParams.subscribe(res => {
          if (res.p_id !== 0) {
              this.p_id = res.p_id;
              let param ;
              if(this.role == 3){
                  param = {p_id: res.p_id,isCloud:true}
              }else{
                  param = {p_id: res.p_id,isCloud:false}
              }

              this.Http.getProjectById(param).subscribe( data => {
                  this.projectParmas.width = data['list'][0]['p_width'];
                  this.projectParmas.height = data['list'][0]['p_height'];
              } );
              this.getPreview(this.p_id, this.pIndex);
          }
      });
  }

  before() {
    if (this.pIndex === 0) {
        return false;
    }

    this.getPreview(this.p_id, --this.pIndex);

  }

  after() {
      if (this.pIndex === 2) {
          return false;
      }
      this.getPreview(this.p_id, ++this.pIndex);
  }

  getPreview(p_id, pi) {
      let param ;
      if(this.role == 3){
          param = {p_id: p_id, next: pi,isCloud:true}
      }else{
          param = {p_id: p_id, next: pi,isCloud:false}
      }
      this.Http.getPreviewScene(param).subscribe( componentList => {
          this.componentList = JSON.parse(componentList['list'][0]['s_data'])['componentList'];
      });
  }

}
